// import Nevow.Athena
// import Divmod.Runtime

Meercat.Job.List = Nevow.Athena.Widget.subclass('Meercat.Job.List');
Meercat.Job.Types = Nevow.Athena.Widget.subclass('Meercat.Job.Types');
Meercat.Job.Status = Nevow.Athena.Widget.subclass('Meercat.Job.Status');

Meercat.Job.List.methods(
function loaded(self) {
    self.refresh();
},
function refresh(self) {
    d = self.callRemote('getJobs');
    d.addCallback(function (result) {
        var ul = jQuery('#' + escapeId(self.node.id) + ' > ul.JobList');
        ul.empty();
        
        for (i = 0; i < result.length; i++) {
            ul.append('<li>' + result[i] + '</li>');
        }
    });
},
function addJobTypeForm(self) {
	var d = self.callRemote('getJobTypeForm');
	
	d.addCallback(function (le) {
		var d2 = self.addChildWidgetFromWidgetInfo(le);
		d2.addCallback(function (widget) {
			jQuery('#main-content').append(widget.node);
		});
	});
	
	d.addErrback(function (failure) {
	    alert('Error creating an add harvester form');
	});
},
function showJobStatus(self, job) {
	var d = self.callRemote('getJobStatusElement', job);
	
	d.addCallback(function (le) {
		var d2 = self.addChildWidgetFromWidgetInfo(le);
		d2.addCallback(function (widget) {
			jQuery('#main-content').append(widget.node);
		});
	});
	
	d.addErrback(function (failure) {
	    alert('Error showing job status: ' + failure);
	});
});

Meercat.Job.Types.methods(
function selectType(self) {
    var d = self.callRemote('getJobForm',
                            self.firstNodeByAttribute('name','type').value);
    
    for (i = 0; i < self.childWidgets.length; i++) {
	    self.childWidgets[i].remove();
	}
    
    d.addCallback(function (result) {
        if (result) {
            var d2 = self.addChildWidgetFromWidgetInfo(result);
            d2.addCallback(function (widget) {
                jQuery('#' + escapeId(self.node.id)).append(widget.node);
            });
        }
    });
    d.addErrback(function (failure) {
        alert('Error selecting the job type');
    });
},
function remove(self) {
    for (i = 0; i < self.childWidgets.length; i++) {
	    self.childWidgets[i].remove();
	}
    
    var d = self.detach();
    d.addCallback(function (result) {
        jQuery('#' + escapeId(self.node.id)).remove();
    });
    
    d.addErrback(function (failure) {
        alert('Error detaching the add harvester form: ' + failure);
    });
});

Meercat.Job.Status.methods(
function loaded(self) {
    self._refreshed = false;
    d = self.refresh();
    self._scheduledCall = false;
    
    d.addCallback(function (result, self) {
        self._refreshed = true;
        self._scheduledCall = setInterval(self.scheduledRefresh,1000);
        return result;
    }, self);
},
function refresh(self) {
    var d = self.callRemote('getStatus');
    var d2 = self.callRemote('getLinks');
    
    d.addCallback(function (result) {
        jQuery('#' + escapeId(self.node.id) + ' div.jobStatus').html(result['status'])
        self._refreshed = true;
        return result;
    });
    
    d2.addCallback(function (result) {
        var list = jQuery('#' + escapeId(self.node.id) + ' ul.actionList');
        list.find('li.statusBased').remove();
        
        for (i = 0; i < result.length; i++) {
            list.append('<li class="statusBased">' + result[i] + '</li>');
        }
        
        return result;
    });
    
    d.addErrback(function (failure) {
        alert('Error getting job status: ' + failure);
    });
    
    d2.addErrback(function (failure) {
        alert('Error updating job status links: ' + failure);
    });
    
    return d;
},
function scheduledRefresh(self) {
    if (self._refreshed) {
        self._refreshed = false;
        var d = self.refresh();
        
        d.addCallback(function (result, self) {
            self._refreshed = true;
            return result;
        }, self);
    }
},
function remove(self) {
    if (self._scheduledCall) {
        clearInterval(self._scheduledCall);
        self._scheduledCall = false;
    }
    var d = self.detach();
    d.addCallback(function (result) {
        jQuery('#' + escapeId(self.node.id)).remove();
    });
    
    d.addErrback(function (failure) {
        alert('Error detaching the job status pane: ' + failure);
    });
},
function startJob(self) {
    var d = self.callRemote('startJob');
    
    d.addErrback(function (failure) {
        alert('Unable to start the job: ' + failure);
    });
},
function stopJob(self) {
    var d = self.callRemote('stopJob');
    
    d.addErrback(function (failure) {
        alert('Unable to stop the job: ' + failure);
    });
},
function deleteJob(self) {
    var d = self.callRemote('deleteJob');
    
    d.addErrback(function (failure) {
        alert('Unable to remove the job: ' + failure);
    });
    
    d.addCallback(function (result) {
        self.widgetParent.refresh();
        self.remove();
    });
});